﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseCardTypes = Wormhole.Engine.CardLibrary.BaseCardTypes;

namespace Wormhole.Engine.Decks
{
	/// <author>Rodney McKay</author>
	/// <date>09 April 2008</date>
	/// <summary>
	/// Cards that are assigned to the current mission.
	/// </summary>
	public class AssignedCards : Deck
	{
		#region Delegates
		/// <summary>
		/// The delegate for the collection changed event.
		/// </summary>
		public delegate void CollectionChangedDelegate();
		#endregion

		#region Events
		/// <summary>
		/// This event is raised when a card is added or removed from the collection.
		/// </summary>
		public event CollectionChangedDelegate CollectionChanged;
		#endregion

		#region Properties
		/// <summary>
		/// The combined skills of the assigned cards.
		/// </summary>
		public CardLibrary.CardProperties.Skills Skills { get; set; }
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public AssignedCards() : base()
		{
			this.Skills = new Wormhole.Engine.CardLibrary.CardProperties.Skills();
		}

		/// <summary>
		/// Create a new deck for a specific player.
		/// </summary>
		/// <param name="player">The player the deck belongs to.</param>
		public AssignedCards(Player.Player player)
			: base(player)
		{
			this.Skills = new Wormhole.Engine.CardLibrary.CardProperties.Skills();
		}
		#endregion	

		#region Set Player Events
		/// <summary>
		/// Wire up the events from other engine objects to listen for.
		/// </summary>
		protected override void SetEngineEvents()
		{
		}
		#endregion

		#region Event Handlers

		#region On Card Add
		/// <summary>
		/// Handle the card add event.
		/// </summary>
		/// <param name="card">The card that was added</param>
		protected override void OnCardAdd(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.CalculateSkills();
			base.OnCardAdd(card);
			this.OnCollectionChanged();
		}
		#endregion

		#region On Card Removed
		/// <summary>
		/// Handle the card removed event.
		/// </summary>
		/// <param name="card">The card that was removed.</param>
		protected override void OnCardRemoved(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.CalculateSkills();
			base.OnCardRemoved(card);
			this.OnCollectionChanged();
		}
		#endregion

		#region On Collection Changed
		/// <summary>
		/// Raise the collection changed event.
		/// </summary>
		protected virtual void OnCollectionChanged()
		{
			if (this.CollectionChanged != null)
				this.CollectionChanged();
		}
		#endregion

		#endregion

		#region Calculate Skills
		/// <summary>
		/// Calculate the new skills from the assigned cards.
		/// </summary>
		protected void CalculateSkills()
		{
			// clear the skills
			this.Skills.Combat = 0;
			this.Skills.Culture = 0;
			this.Skills.Ingenuity = 0;
			this.Skills.Science = 0;

			// calculate the new skills
			foreach (CardLibrary.BaseCardTypes.SkillsCard card in this)
			{
				this.Skills.Combat += card.Skills.Combat == null ? 0 : card.Skills.Combat;
				this.Skills.Culture += card.Skills.Culture == null ? 0 : card.Skills.Culture;
				this.Skills.Ingenuity += card.Skills.Ingenuity == null ? 0 : card.Skills.Ingenuity;
				this.Skills.Science += card.Skills.Science == null ? 0 : card.Skills.Science;
			}
		}
		#endregion
	}
}
